package com.example.hospital.controller;

import com.example.hospital.constant.UserConstants;
import com.example.hospital.dto.LoginForm;
import com.example.hospital.dto.PatientQuery;
import com.example.hospital.dto.Result;
import com.example.hospital.entity.Patient;
import com.example.hospital.service.PatientService;
import com.example.hospital.util.UserHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("patient")
@Tag(name = "Patient", description = "患者相关接口")
public class PatientController {
    @Resource
    private PatientService patientService;

    /**
     * 新增患者
     *
     * @param patient
     * @return patient id or error
     */
    @Operation(
            summary = "新增患者",
            description = "创建一个新的患者记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "添加成功, 返回患者ID"),
                    @ApiResponse(responseCode = "400", description = "请求参数错误")
            }
    )
    @PostMapping
    public Result addPatient(@RequestBody Patient patient) {
        boolean saved = patientService.save(patient);
        return saved ? Result.success("添加成功", patient.getPatientId()) : Result.fail("添加失败");
    }

    /**
     * 根据id删除患者
     *
     * @param id
     * @return
     */
    @Operation(
            summary = "根据id删除患者",
            description = "通过患者ID删除指定的患者记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "删除成功"),
                    @ApiResponse(responseCode = "400", description = "患者未找到")
            }
    )
    @DeleteMapping("/{id}")
    public Result<Integer> deletePatient(@PathVariable("id") Integer id) {
        boolean removed = patientService.removeById(id);
        return removed ? Result.success("删除成功") : Result.fail("删除失败");
    }

    /**
     * 修改患者信息
     *
     * @param patient
     * @return
     */
    @Operation(
            summary = "修改患者信息",
            description = "通过传递新的患者信息更新现有患者记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "修改成功"),
                    @ApiResponse(responseCode = "400", description = "请求参数错误")
            }
    )
    @PutMapping
    public Result updatePatient(@RequestBody Patient patient) {
        boolean updated = patientService.updateById(patient);
        return updated ? Result.success("修改成功") : Result.fail("修改失败");
    }

    /**
     * 根据id查询患者
     *
     * @param id
     * @return patient or error
     */
    @Operation(
            summary = "根据id查询患者",
            description = "通过患者ID查询患者信息",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "400", description = "患者未找到")
            }
    )
    @GetMapping("/{id}")
    public Result<Patient> getPatient(@PathVariable("id") Integer id) {
        Patient patient = patientService.getById(id);
        return patient != null ? Result.success("查询成功", patient) : Result.fail("不存在该患者");
    }


    /**
     * 查询所有患者
     *
     * @return patients list
     */
    @Operation(
            summary = "查询所有患者",
            description = "获取所有患者记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "500", description = "服务器错误")
            }
    )
    @GetMapping
    public Result<List<Patient>> listPatients() {
        return Result.success(patientService.list());
    }

    /**
     * 患者登录
     *
     * @param loginForm
     * @return
     */
    @Operation(
            summary = "患者登录",
            description = "使用手机号和密码进行患者登录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "登录成功, 返回token和name"),
                    @ApiResponse(responseCode = "400", description = "登录失败，参数错误")
            }
    )
    @PostMapping("/login")
    public Result<Map<String, String>> login(@RequestBody LoginForm loginForm) {
        return patientService.login(loginForm);
    }

    /**
     * 获取登录患者信息
     *
     * @return
     */
    @Operation(
            summary = "获取登录患者信息",
            description = "获取当前登录患者的信息",
            responses = {
                    @ApiResponse(responseCode = "200", description = "获取成功"),
                    @ApiResponse(responseCode = "401", description = "未登录")
            },
            parameters = {
                    @Parameter(name = "token", in = ParameterIn.HEADER, required = true)
            }
    )
    @GetMapping("/me")
    public Result<Patient> getLoginPatient() {
        String phone = UserHolder.getUserPhone();
        UserConstants.Role role = UserHolder.getUserRole();
        if (phone == null || role != UserConstants.Role.PATIENT) {
            return Result.fail(401, "未登录");
        }
        Patient patient = patientService.getPatientByPhoneNumber(phone);
        return patient != null ? Result.success(patient) : Result.fail("未找到该患者");
    }

    /**
     * 患者注册
     *
     * @param patient
     * @return
     */
    @Operation(
            summary = "患者注册",
            description = "通过注册信息创建一个新的患者账户",
            responses = {
                    @ApiResponse(responseCode = "200", description = "注册成功"),
                    @ApiResponse(responseCode = "400", description = "注册失败，参数错误")
            }
    )
    @PostMapping("/register")
    public Result register(@RequestBody Patient patient) {
        return patientService.register(patient);
    }

    /**
     * 条件查询患者
     *
     * @param patientQuery
     * @return patients list
     */
    @Operation(
            summary = "条件查询患者",
            description = "根据条件查询患者记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "500", description = "服务器错误")
            }
    )
    @PostMapping("/query")
    public Result<List<Patient>> query(@RequestBody PatientQuery patientQuery) {
        return Result.success(patientService.listPatients(patientQuery));
    }
}
